package io.github.kevinzhwl.edgetts.util;


import io.github.kevinzhwl.edgetts.bean.VoiceItem;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class VoiceManager {
  private List<VoiceItem> voices = new LinkedList<>();


  public VoiceManager(List<VoiceItem> voices) {

    if (voices != null) {
      this.voices.addAll(voices);
    }
  }


  public List<VoiceItem> find(String shortName, String language) {
    if (this.voices.isEmpty()) {
      return new ArrayList<>();
    }

    List<VoiceItem> items = new ArrayList<>();
    voices.forEach(e -> {

      if (shortName != null && StringUtils.equalsIgnoreCase(shortName, e.getShortName())) {
        items.add(e);
      }

      if (language != null && e.getLocale() != null) {
        final String lang = e.getLocale().split("-")[0];
        if (StringUtils.equalsIgnoreCase(lang, language)) {
          items.add(e);
        }
      }
    });


    return items;
  }
}
